/**
 * ...
 * @author Yog
 */

package cookinmaddy.model;

import cookinmaddy.model.Unit;
import cookinmaddy.database.spod.DBUnit;
import cookinmaddy.database.spod.DBUnitManager;
import haxe.Serializer;
import php.Lib;

class UnitManager 
{
	private static var TEMPLATE : String = "unitviewtemplate.mtt";

	public function new(){}
	
	public function getList() : String
	{
		trace("BEGIN");
		try
		{
			trace("get the unit list");
			var unitlist = DBUnit.manager.allUnits(); // _root.getSubUnits();
			trace("call templo");
			trace("END: UnitView::getdisplay <br>");
			return haxe.Serializer.run(unitlist);
		}
		catch (e : String)
		{
			trace("ERROR: " + e);
		}
		trace("END");
		return "";
	}
	
	public function update(value : Unit) : String
	{
		trace("BEGIN");
		try
		{
			var db = DBUnit.manager.get(value.ID);
			db.value = value;
			db.update();
		}
		catch (e : String)
		{
			trace("ERROR: " + e);
		}
		trace("END");
		return "OK";
	}
	
	public function add(value : Unit) : String
	{
		trace("BEGIN");
		try
		{
			var db : DBUnit = new DBUnit();
			db.value = value;
			db.insert();	
			return haxe.Serializer.run(db.value);
		}
		catch (e : String)
		{
			trace("ERROR : " + e);
		}
		trace("END");
		return "KO";
	}	
	
	public function delete(value : Unit) : String
	{
		trace("BEGIN");
		try
		{
			var db = DBUnit.manager.get(value.ID);
			db.value = value;
			db.delete();
			return cast value.ID;
		}
		catch (e : String)
		{
			trace("ERROR : " + e);
		}
		trace("END");
		return "KO";
	}		
}